﻿//-----------------------------------------------------------------------
// <copyright file="TempDataWebflowContextProvider.cs" company="Sławomir Rosiek">
//     Copyright (c) Sławomir Rosiek. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Rosieks.Web.Flow
{
    using System;
    using System.Web.Mvc;

    public class TempDataWebflowContextProvider : IWebflowContextProvider
    {
        private const string WebflowContextKey = "__WebflowContext_{0}__";

        public void SetContext(ControllerContext context, WebflowContext webflowContext)
        {
            if (webflowContext == null)
            {
                throw new ArgumentNullException("webflowContext");
            }

            string key = this.GetKey(webflowContext);

            context.Controller.TempData[key] = webflowContext;
        }

        public void DeleteContext(ControllerContext context, WebflowContext webflowContext)
        {
            if (webflowContext == null)
            {
                throw new ArgumentNullException("webflowContext");
            }

            string key = this.GetKey(webflowContext);

            context.Controller.TempData.Remove(key);
        }

        public WebflowContext GetContext(ControllerContext context, string webflowName)
        {
            string name = string.Format(TempDataWebflowContextProvider.WebflowContextKey, webflowName);

            return context.Controller.TempData[name] as WebflowContext;
        }

        private string GetKey(WebflowContext webflowContext)
        {
            return string.Format(TempDataWebflowContextProvider.WebflowContextKey, webflowContext.Webflow.Name);
        }
    }
}
